﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mogre;

namespace ProjetSMA.View.Mogre
{
    public class Pattern3D
    {
        private Entity3D m_second;
        private SceneManager m_scmgr;
        private string m_name = "_def";
        private Root m_root;
       

        private List<Element3D> m_list_element3D = new List<Element3D>();
        private List<Element3D> m_a_star = new List<Element3D>();
        private List<Element3D> m_perception = new List<Element3D>();
        protected List<Entity> m_list_entity = new List<Entity>();
        private List<SceneNode> m_list_node = new List<SceneNode>();
        private List<string> m_list_node_name = new List<string>();
        private int m_cpt = 0;
        private Random m_rd = new Random();

        public Pattern3D(Root par_root, SceneManager par_sc, string par_name, List<Element3D> par_element, List<Element3D> par_element_perception)
        {
            m_scmgr = par_sc;
            m_name = par_name + m_rd.Next(-999, 999);
            m_root = par_root;
            m_list_element3D = par_element;
            m_cpt = m_rd.Next(-999, 999);
            m_perception = par_element_perception;

            Plane plane;
            plane.normal = Vector3.UNIT_Y;
            plane.d = 100;

            MeshManager.Singleton.CreatePlane("Myplane",
                  ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane,
                  20f, 20f, 10, 10, true, 1, 5, 5, Vector3.UNIT_Z);

        }

        public Pattern3D(Root par_root, SceneManager par_sc, string par_name)
        {
            m_scmgr = par_sc;
            m_name = par_name + m_rd.Next(-999, 999); 
            m_root = par_root;
            m_cpt = m_rd.Next(-999, 999);


            Plane plane;
            plane.normal = Vector3.UNIT_Y;
            plane.d = 100;

            MeshManager.Singleton.CreatePlane("Myplane",
                  ResourceGroupManager.DEFAULT_RESOURCE_GROUP_NAME, plane,
                  20f, 20f, 10, 10, true, 1, 1, 1, Vector3.UNIT_Z);
        }

        public void UpdateConflict()
        {
            m_cpt = m_rd.Next(-999, 999);
            m_name += m_rd.Next(-999, 999); 
        }

        public void Initialisation()
        {

            string loc_name = m_name;
            bool loc_flag = true;

            foreach (Element3D loc_item in m_list_element3D)
            {
                loc_name += "_" + m_cpt;
                SceneNode loc_node = m_scmgr.RootSceneNode.CreateChildSceneNode(loc_name);
                Entity loc_entity = m_scmgr.CreateEntity(loc_name, "Myplane");
                
                loc_node.Position = new Vector3(loc_item.Position.x, 
                                                loc_item.Position.y + loc_item.Coefficient * loc_item.HeightItem + 111.5f, 
                                                loc_item.Position.z);

                loc_entity.SetMaterialName("Examples/TransparentTestBis");
                
                foreach (Element3D loc_item_star in m_a_star)
                {
                    if (loc_item_star == loc_item)
                    {
                        loc_entity.SetMaterialName("Examples/TransparentTestTer");
                    }
                }


                loc_node.AttachObject(loc_entity);
                loc_entity.CastShadows = false;

                m_list_node.Add(loc_node);
                m_list_entity.Add(loc_entity);
                m_list_node_name.Add(loc_name);

                loc_flag = true;
                m_cpt++;
            }

            foreach (Element3D loc_item in m_perception)
            {
                loc_flag = false;

                foreach (Element3D loc_item_base in m_list_element3D)
                {
                    if (loc_item_base == loc_item)
                    {
                        loc_flag = true;
                    }
                }

                if (!loc_flag)
                {

                    loc_name += "_" + m_cpt +"_"+ m_rd.Next(-99, 9999);
                    SceneNode loc_node = m_scmgr.RootSceneNode.CreateChildSceneNode(loc_name);

                    Entity loc_entity = m_scmgr.CreateEntity(loc_name, "Myplane");
                    loc_entity.SetMaterialName("Examples/TransparentTestQuin");

                    loc_node.Position = new Vector3(loc_item.Position.x,
                                                    loc_item.Position.y + loc_item.Coefficient * loc_item.HeightItem + 111.5f,
                                                    loc_item.Position.z);

                    loc_node.AttachObject(loc_entity);
                    loc_entity.CastShadows = false;

                    m_list_node.Add(loc_node);
                    m_list_entity.Add(loc_entity);
                    m_list_node_name.Add(loc_name);

                    m_cpt++;
                }
            }

     
        }

        public void Destroy()
        {

            foreach (string loc_node in m_list_node_name)
            {
                m_scmgr.DestroySceneNode(loc_node);
            }

            foreach (string loc_node in m_list_node_name)
            {
               m_scmgr.DestroyEntity(loc_node);
            }

            m_list_node_name = new List<string>();
            m_list_node = new List<SceneNode>();
            m_list_entity = new List<Entity>();
            m_a_star = new List<Element3D>();
            m_perception = new List<Element3D>();

        }


        public List<Element3D> Element3D
        {
            get { return m_list_element3D; }
            set { m_list_element3D = value; }
        }

        public List<Element3D> ElementsAStar
        {
            get { return m_a_star; }
            set { m_a_star = value; }
        }

        public List<Element3D> ElementsPerception
        {
            get { return m_perception; }
            set { m_perception = value; }
        }
    }
}
